package org.vudroid.core;

import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.vudroid.core.DecodeService;
import org.vudroid.core.codec.CodecContext;
import org.vudroid.core.codec.CodecDocument;
import org.vudroid.core.codec.CodecPage;
import org.vudroid.core.utils.PathFromUri;

/* loaded from: classes2.dex */
public class DecodeServiceBase implements DecodeService {
    public static final String DECODE_SERVICE = "ViewDroidDecodeService";
    private static final int PAGE_POOL_SIZE = 16;
    private final CodecContext codecContext;
    private View containerView;
    private ContentResolver contentResolver;
    private CodecDocument document;
    private boolean isRecycled;
    private final ExecutorService executorService = Executors.newSingleThreadExecutor();
    private final Map<Object, Future<?>> decodingFutures = new ConcurrentHashMap();
    private final HashMap<Integer, SoftReference<CodecPage>> pages = new HashMap<>();
    private Queue<Integer> pageEvictionQueue = new LinkedList();

    public DecodeServiceBase(CodecContext codecContext) {
        this.codecContext = codecContext;
    }

    @Override // org.vudroid.core.DecodeService
    public void setContentResolver(ContentResolver contentResolver) {
        this.contentResolver = contentResolver;
        this.codecContext.setContentResolver(contentResolver);
    }

    @Override // org.vudroid.core.DecodeService
    public void setContainerView(View view) {
        this.containerView = view;
    }

    @Override // org.vudroid.core.DecodeService
    public void open(Uri uri) {
        this.document = this.codecContext.openDocument(PathFromUri.retrieve(this.contentResolver, uri));
    }

    @Override // org.vudroid.core.DecodeService
    public void decodePage(Object obj, int i, DecodeService.DecodeCallback decodeCallback, float f, RectF rectF) {
        final DecodeTask decodeTask = new DecodeTask(i, decodeCallback, f, obj, rectF);
        synchronized (this.decodingFutures) {
            if (this.isRecycled) {
                return;
            }
            Future<?> put = this.decodingFutures.put(obj, this.executorService.submit(new Runnable() { // from class: org.vudroid.core.DecodeServiceBase.1
                @Override // java.lang.Runnable
                public void run() {
                    try {
                        Thread.currentThread().setPriority(4);
                        DecodeServiceBase.this.performDecode(decodeTask);
                    } catch (IOException e) {
                        Log.e(DecodeServiceBase.DECODE_SERVICE, "Decode fail", e);
                    }
                }
            }));
            if (put != null) {
                put.cancel(false);
            }
        }
    }

    @Override // org.vudroid.core.DecodeService
    public void stopDecoding(Object obj) {
        Future<?> remove = this.decodingFutures.remove(obj);
        if (remove != null) {
            remove.cancel(false);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void performDecode(DecodeTask decodeTask) throws IOException {
        if (isTaskDead(decodeTask)) {
            Log.d(DECODE_SERVICE, "Skipping decode task for page " + decodeTask.pageNumber);
            return;
        }
        Log.d(DECODE_SERVICE, "Starting decode of page: " + decodeTask.pageNumber);
        CodecPage page = getPage(decodeTask.pageNumber);
        preloadNextPage(decodeTask.pageNumber);
        if (isTaskDead(decodeTask)) {
            return;
        }
        Log.d(DECODE_SERVICE, "Start converting map to bitmap");
        float calculateScale = calculateScale(page) * decodeTask.zoom;
        Bitmap renderBitmap = page.renderBitmap(getScaledWidth(decodeTask, page, calculateScale), getScaledHeight(decodeTask, page, calculateScale), decodeTask.pageSliceBounds);
        Log.d(DECODE_SERVICE, "Converting map to bitmap finished");
        if (isTaskDead(decodeTask)) {
            renderBitmap.recycle();
        } else {
            finishDecoding(decodeTask, renderBitmap);
        }
    }

    private int getScaledHeight(DecodeTask decodeTask, CodecPage codecPage, float f) {
        return Math.round(getScaledHeight(codecPage, f) * decodeTask.pageSliceBounds.height());
    }

    private int getScaledWidth(DecodeTask decodeTask, CodecPage codecPage, float f) {
        return Math.round(getScaledWidth(codecPage, f) * decodeTask.pageSliceBounds.width());
    }

    private int getScaledHeight(CodecPage codecPage, float f) {
        return (int) (f * codecPage.getHeight());
    }

    private int getScaledWidth(CodecPage codecPage, float f) {
        return (int) (f * codecPage.getWidth());
    }

    private float calculateScale(CodecPage codecPage) {
        return (getTargetWidth() * 1.0f) / codecPage.getWidth();
    }

    private void finishDecoding(DecodeTask decodeTask, Bitmap bitmap) {
        updateImage(decodeTask, bitmap);
        stopDecoding(Integer.valueOf(decodeTask.pageNumber));
    }

    private void preloadNextPage(int i) throws IOException {
        int i2 = i + 1;
        if (i2 >= getPageCount()) {
            return;
        }
        getPage(i2);
    }

    @Override // org.vudroid.core.DecodeService
    public CodecPage getPage(int i) {
        if (!this.pages.containsKey(Integer.valueOf(i)) || this.pages.get(Integer.valueOf(i)).get() == null) {
            this.pages.put(Integer.valueOf(i), new SoftReference<>(this.document.getPage(i)));
            this.pageEvictionQueue.remove(Integer.valueOf(i));
            this.pageEvictionQueue.offer(Integer.valueOf(i));
            if (this.pageEvictionQueue.size() > 16) {
                CodecPage codecPage = this.pages.remove(this.pageEvictionQueue.poll()).get();
                if (codecPage != null) {
                    codecPage.recycle();
                }
            }
        }
        return this.pages.get(Integer.valueOf(i)).get();
    }

    private void waitForDecode(CodecPage codecPage) {
        codecPage.waitForDecode();
    }

    private int getTargetWidth() {
        return this.containerView.getWidth();
    }

    @Override // org.vudroid.core.DecodeService
    public int getEffectivePagesWidth() {
        CodecPage page = getPage(0);
        return getScaledWidth(page, calculateScale(page));
    }

    @Override // org.vudroid.core.DecodeService
    public int getEffectivePagesHeight() {
        CodecPage page = getPage(0);
        return getScaledHeight(page, calculateScale(page));
    }

    @Override // org.vudroid.core.DecodeService
    public int getPageWidth(int i) {
        return getPage(i).getWidth();
    }

    @Override // org.vudroid.core.DecodeService
    public int getPageHeight(int i) {
        return getPage(i).getHeight();
    }

    private void updateImage(DecodeTask decodeTask, Bitmap bitmap) {
        decodeTask.decodeCallback.decodeComplete(bitmap);
    }

    private boolean isTaskDead(DecodeTask decodeTask) {
        boolean z;
        synchronized (this.decodingFutures) {
            z = !this.decodingFutures.containsKey(decodeTask.decodeKey);
        }
        return z;
    }

    @Override // org.vudroid.core.DecodeService
    public int getPageCount() {
        return this.document.getPageCount();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public class DecodeTask {
        private final DecodeService.DecodeCallback decodeCallback;
        private final Object decodeKey;
        private final int pageNumber;
        private final RectF pageSliceBounds;
        private final float zoom;

        private DecodeTask(int i, DecodeService.DecodeCallback decodeCallback, float f, Object obj, RectF rectF) {
            this.pageNumber = i;
            this.decodeCallback = decodeCallback;
            this.zoom = f;
            this.decodeKey = obj;
            this.pageSliceBounds = rectF;
        }
    }

    @Override // org.vudroid.core.DecodeService
    public void recycle() {
        synchronized (this.decodingFutures) {
            this.isRecycled = true;
        }
        for (Object obj : this.decodingFutures.keySet()) {
            stopDecoding(obj);
        }
        this.executorService.submit(new Runnable() { // from class: org.vudroid.core.DecodeServiceBase.2
            @Override // java.lang.Runnable
            public void run() {
                for (SoftReference softReference : DecodeServiceBase.this.pages.values()) {
                    CodecPage codecPage = (CodecPage) softReference.get();
                    if (codecPage != null) {
                        codecPage.recycle();
                    }
                }
                DecodeServiceBase.this.document.recycle();
                DecodeServiceBase.this.codecContext.recycle();
            }
        });
        this.executorService.shutdown();
    }
}
